package com.hp.attendance.util;

import java.awt.*;
import java.util.Random;

public class Code {
    private static int width=100;
    private static int height=50;
    private static String[] fontFamily={"宋体","隶书","楷体","微软雅黑","幼圆","仿宋"};
    private static Random r= new Random();
    private static String s="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    //随机颜色
    public static Color ranDomColor(){
        int red =r.nextInt(256);
        int green =r.nextInt(256);
        int blue =r.nextInt(256);
        return new Color(red,green,blue);
    }

    //随机干扰线
    public static void randomLine(Graphics graphics){
        for (int i = 0; i <8 ; i++) {
            graphics.setColor(ranDomColor());
            int x1 =r.nextInt(width);
            int y1 =r.nextInt(height);
            int x2 =r.nextInt(width);
            int y2=r.nextInt(height);
            graphics.drawLine(x1,y1,x2,y2);
        }
    }
    //随机验证码
    public static String getText(Graphics graphics){
        String code="";
        for (int i = 0; i <4 ; i++) {
            int index=r.nextInt(s.length());
            //验证码
            char c = s.charAt(index);
            //拼接验证码
            code+=c;
            graphics.setColor(ranDomColor());
            //随机字体
            int index2 = r.nextInt(fontFamily.length);
            String zt=fontFamily[index2];
            //随机样式
            int style=r.nextInt(5);
            //随机大小
            int size =r.nextInt(11)+20;
            graphics.setFont(new Font(zt,style,size));

            graphics.drawString(c+"", (int)(i*width*1.0f/4),height-15);

        }
        return code;
    }

}
